package co.fitstart.weapp.api.entity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

import co.fitstart.entity.user.StudentTargetData;
import co.fitstart.entity.user.bwh.BwhRecord;
import co.fitstart.entity.user.inbody.InbodyRecord;

public class CourseAchievement {

    private String name;

    private String avatar;

    /** 累计上课次数 */
    private int finished;

    /** 入营日期 */
    private Date firstScheduleDate;

    /** 学员目标 */
    private StudentTargetData studentTargetData;

    /** 课程有效天数 */
    private long effectiveDay;

    private InbodyRecord startInbody;

    private InbodyRecord endInbody;

    /** 入营照片 */
    private String startPhoto;

    /** 入营照上传时间 */
    private Date startUploadTime;

    /** 结营照片 */
    private String endPhoto;

    /** 结营照上传时间 */
    private Date endUploadTime;

    private BwhRecord startBwh;

    private BwhRecord endBwh;

    public CourseAchievement(String name, String avatar, int finished, Date firstScheduleDate,
            StudentTargetData studentTargetData, long effectiveDay, InbodyRecord startInbody,
            InbodyRecord endInbody, String startPhoto, Date startUploadTime, String endPhoto,
            Date endUploadTime, BwhRecord startBwh, BwhRecord endBwh) {
        this.name = name;
        this.avatar = avatar;
        this.finished = finished;
        this.firstScheduleDate = firstScheduleDate;
        this.studentTargetData = studentTargetData;
        this.effectiveDay = effectiveDay;
        this.startInbody = startInbody;
        this.endInbody = endInbody;
        this.startPhoto = startPhoto;
        this.startUploadTime = startUploadTime;
        this.endPhoto = endPhoto;
        this.endUploadTime = endUploadTime;
        this.startBwh = startBwh;
        this.endBwh = endBwh;
    }

    public String getName() {
        return name;
    }

    public String getAvatar() {
        return avatar;
    }

    public int getFinished() {
        return finished;
    }

    public String getFirstScheduleDate() {
        return firstScheduleDate == null ? null : DateFormatUtils.format(firstScheduleDate, "yyyy.MM.dd");
    }

    /**
     * @return the fullInTarget
     */
    public boolean isFullInTarget() {
        return studentTargetData != null;
    }

    public boolean isDigital() {
        return studentTargetData != null && studentTargetData.getTargetType().isDigital();
    }

    public String getTargetTypeDesc() {
        return studentTargetData == null ? null : studentTargetData.getTargetType().getDescription();
    }

    public String getlatestDate() {
        if(studentTargetData != null && studentTargetData.getTargetType().isDigital()) {
            if(endInbody != null) {
                return endInbody.getRecordTime().substring(0, 4) + "." + endInbody.getRecordTime().substring(5, 7) + "." + endInbody.getRecordTime().substring(8, 10);
            }
        }
        return null;
    }

    public boolean isChanged() {
        if(studentTargetData != null && studentTargetData.getTargetType().isDigital()) {
            if(studentTargetData.getTargetType().isMuscleBuilding()) {
                return startBwh != null && endBwh != null;
            } else {
                return startInbody != null && endInbody != null;
            }
        }
        
        return false;
    }

    public String getWeightChangeDesc() {
        if(studentTargetData != null && studentTargetData.getTargetType().isDigital() && startInbody != null && endInbody != null) {
            if(studentTargetData.getTargetType().isMuscleBuilding() && startInbody.getWeight() < endInbody.getWeight()) {
                return "累计+" + new BigDecimal(endInbody.getWeight() - startInbody.getWeight()).setScale(1, RoundingMode.HALF_UP) + "KG";
            } else if(!studentTargetData.getTargetType().isMuscleBuilding() && startInbody.getWeight() > endInbody.getWeight()) {
                return "累计-" + new BigDecimal(startInbody.getWeight() - endInbody.getWeight()).setScale(1, RoundingMode.HALF_UP) + "KG";
            } else {
                return "累计" + (studentTargetData.getTargetType().isMuscleBuilding() ? "+" : "-") + "0KG";
            }
        } else {
            return null;
        }
    }

    public String getBfrChangeDesc() {
        if(studentTargetData != null && (studentTargetData.getTargetType().isBodyFatRateReducing() || studentTargetData.getTargetType().isMuscleBuilding()) && startInbody != null && endInbody != null) {
            if(startInbody.getWeight() > endInbody.getWeight()) {
                return "累计-" + new BigDecimal((startInbody.getBfr() - endInbody.getBfr()) * 100).setScale(1, RoundingMode.HALF_UP) + "%";
            } else {
                return "累计-0%";
            }
        } else {
            return null;
        }
    }

    public String getTarget() {
        if(studentTargetData != null) {
            if(studentTargetData.getTargetType().isDigital()) {
                return "目标：" + effectiveDay + "天" + studentTargetData.getTarget();
            } else {
                return "目标：" + studentTargetData.getTargetType().getDescription();
            }
        } else {
            return null;
        }
    }

    public BigDecimal getStartBf() {
        return startInbody == null ? null : new BigDecimal(startInbody.getBf()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBfPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBf() < endInbody.getBf()) {
            return new BigDecimal(startInbody.getBf() * 100 / endInbody.getBf()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBfPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBf() > endInbody.getBf()) {
            return new BigDecimal(endInbody.getBf() * 100 / startInbody.getBf()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }
 
    public BigDecimal getEndBf() {
        return endInbody == null ? null : new BigDecimal(endInbody.getBf()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartSm() {
        return startInbody == null ? null : new BigDecimal(startInbody.getSm()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartSmPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getSm() < endInbody.getSm()) {
            return new BigDecimal(startInbody.getSm() * 100 / endInbody.getSm()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndSmPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getSm() > endInbody.getSm()) {
            return new BigDecimal(endInbody.getSm() * 100 / startInbody.getSm()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndSm() {
        return endInbody == null ? null : new BigDecimal(endInbody.getSm()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBfr() {
        return startInbody == null ? null : new BigDecimal(startInbody.getBfr() * 100).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBfrPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBfr() < endInbody.getBfr()) {
            return new BigDecimal(startInbody.getBfr() * 100 / endInbody.getBfr()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBfrPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBfr() > endInbody.getBfr()) {
            return new BigDecimal(endInbody.getBfr() * 100 / startInbody.getBfr()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBfr() {
        return endInbody == null ? null : new BigDecimal(endInbody.getBfr() * 100).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBmi() {
        return startInbody == null ? null : new BigDecimal(startInbody.getBmi()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBmiPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBmi() < endInbody.getBmi()) {
            return new BigDecimal(startInbody.getBmi() * 100 / endInbody.getBmi()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBmiPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBmi() > endInbody.getBmi()) {
            return new BigDecimal(endInbody.getBmi() * 100 / startInbody.getBmi()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBmi() {
        return endInbody == null ? null : new BigDecimal(endInbody.getBmi()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartWhr() {
        return startInbody == null ? null : new BigDecimal(startInbody.getWhr()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartWhrPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getWhr() < endInbody.getWhr()) {
            return new BigDecimal(startInbody.getWhr() * 100 / endInbody.getWhr()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndWhrPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getWhr() > endInbody.getWhr()) {
            return new BigDecimal(endInbody.getWhr() * 100 / startInbody.getWhr()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndWhr() {
        return endInbody == null ? null : new BigDecimal(endInbody.getWhr()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBm() {
        return startInbody == null ? null : new BigDecimal(startInbody.getBm()).setScale(0, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBmPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBm() < endInbody.getBm()) {
            return new BigDecimal(startInbody.getBm() * 100 / endInbody.getBm()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBmPercent() {
        if(startInbody == null || endInbody == null) {
            return new BigDecimal(0);
        } else if(startInbody.getBm() > endInbody.getBm()) {
            return new BigDecimal(endInbody.getBm() * 100 / startInbody.getBm()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBm() {
        return  endInbody == null ? null : new BigDecimal(endInbody.getBm()).setScale(0, RoundingMode.HALF_UP);
    }

    public String getStartPhoto() {
        return startPhoto;
    }

    /**
     * @return the startUploadTime
     */
    public Date getStartUploadTime() {
        return startUploadTime;
    }

    public String getEndPhoto() {
        return endPhoto;
    }

    /**
     * @return the endUploadTime
     */
    public Date getEndUploadTime() {
        return endUploadTime;
    }

    public BigDecimal getStartBust() {
        return startBwh == null ? null : new BigDecimal(startBwh.getBust()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartBustPercent() {
        if(startBwh == null || endBwh == null) {
            return new BigDecimal(0);
        } else if(startBwh.getBust() < endBwh.getBust() && Math.abs(endBwh.getBust()) > 0) {
            return new BigDecimal(startBwh.getBust() * 100 / endBwh.getBust()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBustPercent() {
        if(startBwh == null || endBwh == null) {
            return new BigDecimal(0);
        } else if(startBwh.getBust() > endBwh.getBust() && Math.abs(startBwh.getBust()) > 0) {
            return new BigDecimal(endBwh.getBust() * 100 / startBwh.getBust()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndBust() {
        return endBwh == null ? null : new BigDecimal(endBwh.getBust()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartWaist() {
        return startBwh == null ? null : new BigDecimal(startBwh.getWaist()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartWaistPercent() {
        if(startBwh == null || endBwh == null) {
            return new BigDecimal(0);
        } else if(startBwh.getWaist() < endBwh.getWaist() && Math.abs(endBwh.getWaist()) > 0) {
            return new BigDecimal(startBwh.getWaist() * 100 / endBwh.getWaist()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndWaistPercent() {
        if(startBwh == null || endBwh == null) {
            return new BigDecimal(0);
        } else if(startBwh.getWaist() > endBwh.getWaist() && Math.abs(startBwh.getWaist()) > 0) {
            return new BigDecimal(endBwh.getWaist() * 100 / startBwh.getWaist()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndWaist() {
        return endBwh == null ? null : new BigDecimal(endBwh.getWaist()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartHips() {
        return startBwh == null ? null : new BigDecimal(startBwh.getHips()).setScale(1, RoundingMode.HALF_UP);
    }

    public BigDecimal getStartHipsPercent() {
        if(startBwh == null || endBwh == null) {
            return new BigDecimal(0);
        } else if(startBwh.getHips() < endBwh.getHips() && Math.abs(endBwh.getHips()) > 0) {
            return new BigDecimal(startBwh.getHips() * 100 / endBwh.getHips()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndHipsPercent() {
        if(startBwh == null || endBwh == null ) {
            return new BigDecimal(0);
        } else if(startBwh.getHips() > endBwh.getHips() && Math.abs(startBwh.getHips()) > 0) {
            return new BigDecimal(endBwh.getHips() * 100 / startBwh.getHips()).setScale(2, RoundingMode.HALF_UP);
        } else {
            return new BigDecimal(100);
        }
    }

    public BigDecimal getEndHips() {
        return endBwh == null ? null : new BigDecimal(endBwh.getHips()).setScale(1, RoundingMode.HALF_UP);
    }

    public boolean isSharable() {
        return studentTargetData != null;
    }

    public BigDecimal getStartWeight() {
        if(studentTargetData != null && studentTargetData.getTargetType().isDigital() && startInbody != null) {
            return new BigDecimal(startInbody.getWeight()).setScale(1, RoundingMode.HALF_UP);
        } else {
            return null;
        }
    }

    public BigDecimal getWeightChangePercent() {
        if(studentTargetData != null && studentTargetData.getTargetType().isDigital() && startInbody != null && endInbody != null) {
            if(studentTargetData.getTargetType().isMuscleBuilding() && startInbody.getWeight() < endInbody.getWeight()) {
                if((endInbody.getWeight() - startInbody.getWeight()) > studentTargetData.getWeightChange()) {
                    return new BigDecimal(100);
                } else {
                    return new BigDecimal((endInbody.getWeight() - startInbody.getWeight()) * 100 / studentTargetData.getWeightChange()).setScale(2, RoundingMode.HALF_UP);
                }
            } else if(!studentTargetData.getTargetType().isMuscleBuilding() && (studentTargetData.getWeightChange() != null && studentTargetData.getWeightChange() != 0) && startInbody.getWeight() > endInbody.getWeight()) {
                if((startInbody.getWeight() - endInbody.getWeight()) > studentTargetData.getWeightChange()) {
                    return new BigDecimal(100);
                } else {
                    return new BigDecimal((startInbody.getWeight() - endInbody.getWeight()) * 100 / studentTargetData.getWeightChange()).setScale(2, RoundingMode.HALF_UP);
                }
            } else {
                return new BigDecimal(0);
            }
        } else {
            return null;
        }
    }

    public BigDecimal getEndWeight() {
        if(studentTargetData != null && studentTargetData.getTargetType().isDigital() && endInbody != null) {
            return new BigDecimal(endInbody.getWeight()).setScale(1, RoundingMode.HALF_UP);
        } else {
            return null;
        }
    }

    public BigDecimal getBfrChangePercent() {
        if(studentTargetData != null && (studentTargetData.getTargetType().isBodyFatRateReducing() || studentTargetData.getTargetType().isMuscleBuilding()) && startInbody != null && endInbody != null) {
            if(startInbody.getBfr() > endInbody.getBfr()) {
                if((startInbody.getBfr() - endInbody.getBfr()) * 100 > studentTargetData.getBfrChange()) {
                    return new BigDecimal(100);
                } else {
                    return new BigDecimal((startInbody.getBfr() - endInbody.getBfr()) * 10000 / studentTargetData.getBfrChange()).setScale(2, RoundingMode.HALF_UP);
                }
            } else {
                return new BigDecimal(0);
            }
        } else {
            return null;
        }
    }

    public boolean isBfrChangeShow() {
        return studentTargetData != null && (studentTargetData.getTargetType().isBodyFatRateReducing() || studentTargetData.getTargetType().isMuscleBuilding()) && startInbody != null && endInbody != null;
    }

    public boolean isWeightChangeShow() {
        return studentTargetData != null && (studentTargetData.getTargetType().isMuscleBuilding() || (studentTargetData.getTargetType().isBodyFatRateReducing() && studentTargetData.getWeightChange() != null && studentTargetData.getWeightChange() != null)) && startInbody != null && endInbody != null;
    }
}
